﻿Function Get-Project($organizationId,$credential)
{
    try
    {
        $projects=Invoke-RestMethod -Method Get -Uri https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/ -Credential $credential
    }
    catch
    {
        $exception=$_
        Write-Host "Error occured while attempting to retrieve the card"
        $encounteredError=$true
    }
    #API Throttle (100 milli but adding a margin)
    Start-Sleep -Milliseconds 200
    $projects.SyncRoot
}

Function Get-ProjectIteration($organizationId,$projectName,$projectId,$credential)
{
    if($projectId -eq $null -or $projectId -eq "")
    {
        $projectId=(Get-Project -credential $credential -organizationId $organizationId | Where-Object {$_.name -eq $projectName}).slug
    }

    try
    {
        $projects=Invoke-RestMethod -Method Get -Uri https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/$projectId/iterations -Credential $credential
    }
    catch
    {
        $exception=$_
        Write-Host "Error occured while attempting to retrieve the card"
        $encounteredError=$true
    }
    #API Throttle (100 milli but adding a margin)
    Start-Sleep -Milliseconds 200

    $projects
}

Function Get-ProjectNewsFeed($organizationId,$projectId,$credential)
{
    $newsFeedItems=@()
    try
    {
        $newsFeed=Invoke-RestMethod -Method Get -Uri "https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/$projectId/newsfeed" -Credential $credential
        $totalPages=$newsFeed.max_page
        for($pageCounter=1;$pageCounter -le $totalPages;$pageCounter++)
        {
            if($pageCounter -gt 1)
            {
               Start-Sleep -Milliseconds 200
               $newsFeed=Invoke-RestMethod -Method Get -Uri "https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/$projectId/newsfeed?page=$pageCounter" -Credential $credential
            }

            foreach($newsFeedItem in $newsFeed.Items)
            {
                $newsFeedItems+=$newsFeedItem
            }
        }
    }
    catch
    {
        $exception=$_
        Write-Host "Error occured while attempting to retrieve the card"
        $encounteredError=$true
    }
    #API Throttle (100 milli but adding a margin)
    Start-Sleep -Milliseconds 200
    $newsFeedItems
}

Function Get-BoardCard($organizationId,$projectName,$projectId,$storyId,$tags,$credential)
{
    
    if($projectId -eq $null -or $projectId -eq "")
    {
        $projectId=(Get-Project -credential $credential -organizationId $organizationId | Where-Object {$_.name -eq $projectName}).slug
    }
    
    if($storyId -ne $null)
    {
        try
        {    
            $cards=Invoke-RestMethod -Method Get -Uri "https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/$projectId/stories/$storyId" -Credential $credential
        }
        catch
        {
            $exception=$_
            Write-Host "Error occured while attempting to retrieve the card"
            $encounteredError=$true
        }
    }
    elseif($tags -ne $null)
    {
        $query=[System.Web.HttpUtility]::UrlEncode("tag:$tags")
        $cards=@()
        try
        {
            $results=Invoke-RestMethod -Method Get -Uri "https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/$projectId/search?q=$query" -Credential $credential

            $totalPages=$results.max_page
            for($pageCounter=1;$pageCounter -le $totalPages;$pageCounter++)
            {
                if($pageCounter -gt 1)
                {
                   Start-Sleep -Milliseconds 200
                   $results=Invoke-RestMethod -Method Get -Uri "https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/$projectId/search?q=$query&page=$pageCounter" -Credential $credential
                }

                foreach($card in $results.Items)
                {
                    $cards+=$card
                }
            }

        }
        catch
        {
            $exception=$_
            Write-Host "Error occured while attempting to search for cards"
            $encounteredError=$true
        }
    }
    else
    {
        try
        {
            $cards=@()

            $results=Invoke-RestMethod -Method Get -Uri "https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/$projectId/stories" -Credential $credential

            $totalPages=$results.max_page
            for($pageCounter=1;$pageCounter -le $totalPages;$pageCounter++)
            {
                if($pageCounter -gt 1)
                {
                   Start-Sleep -Milliseconds 200
                   $results=Invoke-RestMethod -Method Get -Uri "https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/$projectId/stories?page=$pageCounter" -Credential $credential
                }

                foreach($card in $results.Items)
                {
                    $cards+=$card
                }
            }
        }
        catch
        {
            $exception=$_
            Write-Host "Error occured while attempting to retrieve the cards for a project"
            $encounteredError=$true
        }
        
    }
    #API Throttle (100 milli but adding a margin)
    Start-Sleep -Milliseconds 200

    if($cards.Items -ne $null -and $cards.count -gt 0 )
    {
        $cards.Items
    }
    elseif($cards.id -ne $null)
    {
        $cards
    }
}



Function Convert-ToPointScale($effort)
{
    
    if($effort -le 0.5)
    {
        $points=0.5
    }
    elseif($effort -gt 0.5 -and $effort -le 1)
    {
        $points=1
    }
    elseif($effort -gt 1 -and $effort -le 2)
    {
        $points=2
    }
    elseif($effort -gt 2 -and $effort -le 3)
    {
        $points=3
    }
    elseif($effort -gt 3 -and $effort -le 5)
    {
        $points=5
    }
    elseif($effort -gt 5 -and $effort -le 8)
    {
        $points=8
    }
    elseif($effort -gt 8 -and $effort -le 13)
    {
        $points=13
    }
    elseif($effort -gt 13 -and $effort -le 20)
    {
        $points=20
    }
    elseif($effort -gt 20 -and $effort -le 40)
    {
        $points=40
    }
    elseif($effort -gt 40 -and $effort -le 100)
    {
        $points=100
    }
    else{$points=0}
    
    $points
}

Function New-BoardCard($organizationId,$title,$description,$category,$cardType,$estimate,$assignees,$tags,[Hashtable]$customFields,$projectName,$projectId,$iterationId,$iterationName,$credential)
{
    if($projectId -eq $null -or $projectId -eq "")
    {
        $projectId=(Get-Project -credential $credential -organizationId $organizationId | Where-Object {$_.name -eq $projectName}).slug
    }

    if($iterationId -eq $null -or $iterationId -eq "")
    {
        $iterationId=(Get-ProjectIteration -projectName $projectName -credential $credential -organizationId $organizationId | Where-Object {$_.name -eq $iterationName} ).id
    }

    $url="https://www.scrumdo.com/api/v2/organizations/gem-it/projects/$projectId/iterations/$iterationId/stories"
    

    $body = @{
        rank = 1
        category=$category
        status_text=$cardType
        detail= $description
        status= 1
        summary= $title
        points = $estimate
        assignees=$assignees
        tags=$tags

    }
    
    if($customFields -ne $null)
    {
        foreach($customField in $customFields.GetEnumerator())
        {
            $body.Add($customField.Name,$customField.Value)    
        }
    }

    #Write-Host $url
    #Write-Host $body
    try
    {
        $output=Invoke-RestMethod -Method Post -Uri $url -Body $body -Credential $credential
    }
    catch
    {
        $exception=$_
        Write-Host "Error occured while attempting to update the card"
        $encounteredError=$true
    }

    #API Throttle (500 milli but adding a margin)
    Start-Sleep -Milliseconds 1500

    $encounteredError

}


Function Update-BoardCard($organizationId,$storyId,$title,$description,$category,$cardType,$rank,$status,$estimate,$assignees,$tags,[Hashtable]$customFields,$projectName,$projectId,$iterationId,$iterationName,$credential)
{
    if($projectId -eq $null -or $projectId -eq "")
    {
        $projectId=(Get-Project -credential $credential -organizationId $organizationId | Where-Object {$_.name -eq $projectName}).slug
    }

    if($iterationId -eq $null -or $iterationId -eq "")
    {
        $iterationId=(Get-ProjectIteration -projectName $projectName -credential $credential -organizationId $organizationId | Where-Object {$_.name -eq $iterationName} ).id
    }

    $url="https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/$projectId/iterations/$iterationId/stories/$storyId"
    
    #If the request is done, put it on the top in the Done column
    if($status -eq 10)
    {
        $rank = 1
    }
   
    $body = @{
        rank = $rank
        category=$category
        status_text=$cardType
        detail= $description
        status= $status
        summary= $title
        points = $estimate
        assignees=$assignees
        tags=$tags
    }

    if($customFields -ne $null)
    {
        foreach($customField in $customFields.GetEnumerator())
        {
            $body.Add($customField.Name,$customField.Value)    
        }
    }
    

    #Write-Host $url
    #Write-Host $body
    try
    {
        $output=Invoke-RestMethod -Method Put -Uri $url -Body $body -Credential $credential
    }
    catch
    {
        $exception=$_
        Write-Host "Error occured while attempting to update the card"
        $encounteredError=$true
    }

    #API Throttle (500 milli but adding a margin)
    Start-Sleep -Milliseconds 750

    $encounteredError
}

Function Remove-BoardCard($organizationId,$storyId,$projectName,$projectId,$credential)
{
    if($projectId -eq $null -or $projectId -eq "")
    {
        $projectId=(Get-Project -credential $credential -organizationId $organizationId | Where-Object {$_.name -eq $projectName}).slug
    }

    $url="https://www.scrumdo.com/api/v2/organizations/$organizationId/projects/$projectId/stories/$storyId"
    

    try
    {
        $output=Invoke-RestMethod -Method Delete -Uri $url -Credential $credential
        Write-Host "Card deleted"
    }
    catch
    {
        $exception=$_
        Write-Host "Error occured while attempting to update the card"
        $encounteredError=$true
    }

    #API Throttle (500 milli but adding a margin)
    Start-Sleep -Milliseconds 750

    $encounteredError
}